package com.example.demo.controller;

import com.example.demo.dto.ResponseResult;
import com.example.demo.model.City;
import com.example.demo.model.CityImage;
import com.example.demo.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/cities")
@CrossOrigin(origins = "*")
public class CityController {

    @Autowired
    private CityService cityService;
    @GetMapping("/{id}")
    public ResponseResult<City> getCityById(@PathVariable Long id) {
        try {
            City city = cityService.getCityWithImagesById(id);
            return ResponseResult.success(city);
        } catch (RuntimeException e) {
            return ResponseResult.error(404, e.getMessage());
        } catch (Exception e) {
            return ResponseResult.error("查询城市详情失败");
        }
    }

    @GetMapping
    public ResponseResult<List<City>> getAllCities() {
        try {
            List<City> cities = cityService.getAllCitiesWithImages();
            return ResponseResult.success(cities);
        } catch (Exception e) {
            return ResponseResult.error("获取城市列表失败");
        }
    }

    @GetMapping("/search")
    public ResponseResult<List<City>> searchCities(@RequestParam String name) {
        try {
            List<City> cities = cityService.searchCitiesWithImages(name);
            return ResponseResult.success(cities);
        } catch (Exception e) {
            return ResponseResult.error("搜索城市失败");
        }
    }

    @PostMapping
    public ResponseResult<City> createCity(@RequestBody City city) {
        try {
            List<CityImage> images = city.getImages();
            City createdCity = cityService.createCityWithImages(city, images);
            return ResponseResult.success("城市创建成功", createdCity);
        } catch (Exception e) {
            return ResponseResult.error(500, "创建城市失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public ResponseResult<City> updateCity(@PathVariable Long id, @RequestBody City city) {
        try {
            List<CityImage> images = city.getImages();
            City updatedCity = cityService.updateCityWithImages(id, city, images);
            return ResponseResult.success("城市更新成功", updatedCity);
        } catch (RuntimeException e) {
            return ResponseResult.error(404, e.getMessage());
        } catch (Exception e) {
            return ResponseResult.error(500, "更新城市失败: " + e.getMessage());
        }
    }

    @PatchMapping("/{id}")
    public ResponseResult<City> updateCityBasicInfo(@PathVariable Long id, @RequestBody City city) {
        try {
            city.setId(id);
            City updatedCity = cityService.updateCityBasicInfo(city);
            return ResponseResult.success("城市信息更新成功", updatedCity);
        } catch (RuntimeException e) {
            return ResponseResult.error(404, e.getMessage());
        } catch (Exception e) {
            return ResponseResult.error(500, "更新城市信息失败");
        }
    }

    @DeleteMapping("/{id}")
    public ResponseResult<String> deleteCity(@PathVariable Long id) {
        try {
            cityService.deleteCityWithImages(id);
            return ResponseResult.success("城市删除成功");
        } catch (RuntimeException e) {
            return ResponseResult.error(404, e.getMessage());
        } catch (Exception e) {
            return ResponseResult.error(500, "删除城市失败");
        }
    }

}
